﻿using MediatR;
using T.Materials.Domain.Dto;
using T.Materials.Read.API.ApplicationRead.Command.ExamineAndApproveCommand;
using T.Materials.Repository.MaterialsInterface;
using static T.Materials.Domain.Enum.Enums;

namespace T.Materials.Read.API.ApplicationRead.CommandHandler.ExamineAndApproveCommandHadnler
{
    /// <summary>
    /// 物资采购审批命令处理器
    /// </summary>
    public class ExamineAndApproveMaterialStatisticsCommandHandler : IRequestHandler<ExamineAndApproveMaterialStatisticsCommand, ListMaterialStoreDto<MaterialStoreDto>>
    {
        /// <summary>
        /// 物资与入库仓储接口
        /// </summary>
        protected readonly IMaterialStoreRepository materialStoreRepository;

        /// <summary>
        /// 国家仓储接口
        /// </summary>
        protected readonly ICountryRepository countryRepository;

        /// <summary>
        /// 省份仓储接口
        /// </summary>
        protected readonly IEconomizeRepository economizeRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="materialStoreRepository">物资与入库仓储接口</param>
        /// <param name="countryRepository">国家仓储接口</param>
        /// <param name="economizeRepository">省份仓储接口</param>
        public ExamineAndApproveMaterialStatisticsCommandHandler(IMaterialStoreRepository materialStoreRepository, ICountryRepository countryRepository, IEconomizeRepository economizeRepository)
        {
            this.materialStoreRepository = materialStoreRepository;
            this.countryRepository = countryRepository;
            this.economizeRepository = economizeRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ListMaterialStoreDto<MaterialStoreDto>> Handle(ExamineAndApproveMaterialStatisticsCommand request, CancellationToken cancellationToken)
        {
            var list = from a in await materialStoreRepository.GetAllAsync()
                       join b in await countryRepository.GetAllAsync()
                       on a.CountryId equals b.CountryId
                       join c in await economizeRepository.GetAllAsync()
                       on b.CountryId equals c.CountryId
                       where !a.IsDeleted && a.MaterialClassificat == 1 && a.ApprovalStatus!=ApprovalStatus.待提交 && a.CountryId == b.CountryId && a.EconomizeId == c.EconomizeId
                       select new MaterialStoreDto
                       {
                           MaterialStoreId = a.MaterialStoreId,
                           CompanyID = a.CompanyID,
                           User_Id = a.User_Id,
                           NameOfGoods = a.NameOfGoods,
                           MaterialStoreType = a.MaterialStoreType,
                           Material = a.Material,
                           Specifications = a.Specifications,
                           CountryId = a.CountryId,
                           CountryName = b.CountryName,
                           EconomizeId = a.EconomizeId,
                           EconomizeName = c.EconomizeName,
                           Number = a.Number,
                           DeletedDate = a.DeletedDate,
                           UsageDescription = a.UsageDescription,
                           Applicant = a.Applicant,
                           ApprovalStatus = a.ApprovalStatus,
                           Approver = a.Approver,
                           Registrant = a.Registrant,
                           PaymentMethod = a.PaymentMethod,
                           UnitPrice = a.UnitPrice,
                           Notes = a.Notes,
                           MaterialClassificat = a.MaterialClassificat,
                           CreatedBy = a.CreatedBy,
                           CreatedDate = a.CreatedDate,
                           UpdatedBy = a.UpdatedBy,
                           UpdatedDate = a.UpdatedDate,
                           DeletedBy = a.DeletedBy,
                           DeliveryDate = a.DeliveryDate,
                           IsDeleted = a.IsDeleted,
                       };


            if (!string.IsNullOrEmpty(request.NameOfGoods))
            {
                list = list.Where(x => x.NameOfGoods.Contains(request.NameOfGoods));
            }

            if (!string.IsNullOrEmpty(request.Material))
            {
                list = list.Where(x => x.Material.Contains(request.Material));
            }

            if (request.CountryId > 0)
            {
                list = list.Where(x => x.CountryId == request.CountryId);
            }

            if (request.EconomizeId > 0)
            {
                list = list.Where(x => x.EconomizeId == request.EconomizeId);
            }

            if (!string.IsNullOrEmpty(request.Applicant))
            {
                list = list.Where(x => x.Applicant.Contains(request.Applicant));
            }

            if (request.ApprovalStatus == ApprovalStatus.待审批)
            {
                list = list.Where(o => o.ApprovalStatus == ApprovalStatus.待审批);
            }
            if (request.ApprovalStatus == ApprovalStatus.已通过)
            {
                list = list.Where(o => o.ApprovalStatus != ApprovalStatus.待审批);
            }

            if (request.pageSize == null)
            {
                request.pageSize = list.Count();
            }
            if (request.pageIndex == null)
            {
                request.pageIndex = 1;
            }

            var res = new ListMaterialStoreDto<MaterialStoreDto>();
            var index = request.pageIndex;
            var size = request.pageSize;
            res.totalCount = list.Count();
            res.pageCount = (int)Math.Ceiling((res.totalCount * 1.0) / (int)size);
            res.list = list.OrderByDescending(x => x.CreatedDate).Skip(((int)index - 1) * (int)size).Take((int)size).ToList();
            return res;
        }
    }
}
